<?php
//图片缩略图模块
namespace Kuoqi\FileDriver;
use Kuoqi\Config;
use Kuoqi\Params;
use Kuoqi\Route;

/**
 * 图片的常用处理对象信息
 * Class Image
 * @package Kuoqi\FileDriver
 * User kuoqi
 * Datetime 2022/4/1 11:46
 * Remark
 */
class Image{



    /**
     * 当前原始输入文件
     * @var string
     * User kuoqi
     * Datetime 2022/4/1 11:32
     * Remark
     */
    protected $file = '';

    /**
     * 是否启用成功
     * @var bool
     * User kuoqi
     * Datetime 2022/4/1 11:32
     * Remark
     */
    protected $is_valied = true;

    /**
     * 错误信息模块
     * @var string
     * User kuoqi
     * Datetime 2022/4/1 11:32
     * Remark
     */
    protected $errmsg = '';

    /**
     * 当前的图片资源
     * @var null
     * User kuoqi
     * Datetime 2022/4/1 12:22
     * Remark
     */
    protected $source = null;

    /**
     * 输出图像资源
     * @var string
     * User kuoqi
     * Datetime 2022/4/1 12:28
     * Remark
     */
    protected $image = '';


    /**
     * 图像宽度
     * @var int
     * User kuoqi
     * Datetime 2022/4/1 12:37
     * Remark
     */
    protected $width = 0;

    /**
     * 图像高度
     * @var int
     * User kuoqi
     * Datetime 2022/4/1 12:38
     * Remark
     */
    protected $height = 0;

    /**
     * 背景图颜色
     * @var string
     * User kuoqi
     * Datetime 2022/4/1 15:44
     * Remark
     */
    protected $img_background_color = '';

    /**
     * 默认缓存目录的数据信息
     * @var string
     * User kuoqi
     * Datetime 2022/4/2 16:53
     * Remark
     */
    protected $tmp_path = '';

    protected $_thumb = null;//缩略图的暂时信息

    /**
     * 使用一个基础的文件，支持@APP,@WEB，@ROOT....
     * Thumb constructor.
     * @param $file
     */
    public function __construct($file = '')
    {
        $this->input($file);//获取初始文件输入
        //输出缓存默认目录
        $this->tmp_path = Config::get('IMAGE_TMP_PATH');
    }

    /**
     * 获取初始文件输入
     * User kuoqi
     * Datetime 2022/4/1 11:35
     * Remark
     */
    public function input($file){
        //自动判定是否为web信息
        if(preg_match('/^http/',$file)){
            //网络资源
        }else{
            //本地资源
            $file = \Kuoqi\Route::str2rootpath($file);
        }
        $this->file = $file;
        $content = @file_get_contents($file);
        if($content){
            //这个时候就使用GD2的方式把图片处理了
            $this->source = imagecreatefromstring($content);
            //获取当前图片的大小及初始化的数据信息
            $this->width = imagesx($this->source);
            $this->height = imagesy($this->source);
            $this->is_valied = true;
            $this->errmsg = '';
        }else{
            $this->is_valied = false;
            $this->errmsg = '文件资源不存在';
        }
        return ;
    }

    /**
     * 使用缩略图的模式
     * createby kuoqi at 2022/9/21 14:23
     * @param $thumb_w
     * @param $thumb_h
     * @param int $thumb_mode
     * @return string
     */
    public function thumb($thumb_w,$thumb_h,$thumb_mode = 0,$downmodel = 'F'){
        //使用缩略图
        $this->_thumb = new ImageThumb();
        $this->_thumb->inputWithSource($this->source);
        $this->_thumb->setSize($thumb_w,$thumb_h);
        $this->_thumb->mode($thumb_mode);
        //配置一个文件临时路径
        $file = '{@WEB}/upload/tmp/'.date('Ymd').'/'.md5(time().rand(1000,9999)).'.png';
        return $this->_thumb->toThumb($downmodel,$file);
    }

    /**
     * 获取当前资源
     * @return null
     * User kuoqi
     * Datetime 2022/4/1 13:57
     * Remark
     */
    public function getSource(){
        return $this->source;
    }

    /**
     * 获取当前image
     * @return string
     * User kuoqi
     * Datetime 2022/4/1 13:57
     * Remark
     */
    public function getImage(){
        return $this->image;
    }

    /**
     * 获取输入图片资源宽度
     * @return int
     * User kuoqi
     * Datetime 2022/4/1 14:01
     * Remark
     */
    public function getWidth(){
        return $this->width;
    }

    /**
     * 获取输入图片资源高度
     * @return int
     * User kuoqi
     * Datetime 2022/4/1 14:01
     * Remark
     */
    public function getHeight(){
        return $this->height;
    }

    public function flush(){
        //根据当前配置执行冲刷信息
        $this->image = $this->source;
    }

    /**
     * 输出为png图片
     * User kuoqi
     * Datetime 2022/4/1 16:52
     * Remark
     */
    public function toPng(){
        $this->flush();
        if(!$this->image){
            return ;
        }
        ob_end_clean();
        header('content-type:image/png');
        imagepng($this->image);
        imagedestroy($this->image);
        die();
    }

    /**
     * 输出为一个链接地址
     * @return string|void
     * User kuoqi
     * Datetime 2022/4/1 16:52
     * Remark
     */
    public function toStream(){
        $this->flush();
        if(!$this->image){
            return ;
        }
        ob_clean();
        ob_start();
            imagepng($this->image);
            imagedestroy($this->image);
        $img = ob_get_clean();//获取缓冲区内容
        $imgInfo = 'data:png;base64,'.chunk_split(base64_encode($img));//转base64
		return $imgInfo;
    }

    /**
     * 输出一个base64字符串
     * @return string|void
     * User kuoqi
     * Datetime 2022/5/12 14:22
     * Remark
     */
    public function toBase64(){
        $this->flush();
        if(!$this->image){
            return ;
        }
        ob_clean();
        ob_start();
        imagepng($this->image);
        imagedestroy($this->image);
        $img = ob_get_clean();//获取缓冲区内容
        $imgInfo = 'data:png;base64,'.base64_encode($img);//转base64
        return $imgInfo;
    }

    /**
     * 转换为path 输出
     * @param string $path
     * @param string $basename
     * @return string|void
     * User kuoqi
     * Datetime 2022/4/20 15:00
     * Remark
     */
    public function toPath($path = '',$basename = ''){
        if(!$path){
            $path = $this->tmp_path;
        }
        $this->flush();
        if(!$this->image){
            return ;
        }
        $path = Route::str2rootpath($path);
        is_dir($path)||@mkdir($path,777,true);
        $file = $path .Params::$DS.'_'.($basename?$basename:basename($this->file));
        imagepng($this->image,$file);
        imagedestroy($this->image);
        return $file;
    }

    /**
     * 转换为可以用的url
     * @param $server
     * @param $path
     * @param string $basename
     * @return mixed|void
     * User kuoqi
     * Datetime 2022/4/20 15:03
     * Remark
     */
    public function toUrl($server,$path,$basename = ''){
        $path = Route::str2rootpath($path);
        $file = $path .Params::$DS. '_' .($basename?$basename:basename($this->file));
        if(is_file($file)){
            $web = Config::get('HOME');
            $url =  str_replace($web,$server,$file);
            $url = str_replace(Params::$DS,Params::$LL,$url);
            return $url;
        }
        is_dir($path)||@mkdir($path,777,true);
        $this->flush();
        if(!$this->image){
            return ;
        }

        imagepng($this->image,$file);
        imagedestroy($this->image);
        $web = Config::get('HOME');
        $url =  str_replace($web,$server,$file);
        $url = str_replace(Params::$DS,Params::$LL,$url);
        return $url;
    }

    public function isValied(){
        return $this->is_valied;
    }

    /**
     * 外部设置错误信息
     * @param $errmsg
     * User kuoqi
     * Datetime 2022/5/10 10:34
     * Remark
     */
    public function setError($errmsg){
        $this->is_valied = false;
        $this->errmsg = $errmsg;
    }

    /**
     * 外部获取错误信息
     * @return string
     * User kuoqi
     * Datetime 2022/5/10 10:34
     * Remark
     */
    public function getError(){
        return $this->errmsg;
    }

}